package morenoapp.components.thinkgraphv2.nodes;

import java.awt.BasicStroke;
import java.awt.Stroke;
import java.awt.geom.Point2D;
import java.beans.PropertyChangeEvent;
import morenoapp.components.thinkgraphv2.nodes.partial.PVector;
import morenoapp.model.enums.MorenoQuestionType;
import morenoapp.model.data.MorenoAnswerItemFace;
import morenoapp.model.data.MorenoQuestionFace;
import morenoapp.model.data.MorenoRespondentFace;
import morenoapp.model.view2.MorenoView2Face;

/**
 *
 * @author root
 */
public class RelationshipItemNode extends VectorItemNode {
    final MorenoAnswerItemFace answerItem;
    final MorenoRespondentFace source;
    final MorenoRespondentFace target;
    final MorenoQuestionFace question;
    final MorenoQuestionType type;
    PVector vector;

    public RelationshipItemNode(MorenoView2Face _filter, MorenoAnswerItemFace _answerItem) {
        super(_filter);
        this.answerItem = _answerItem;
        this.source = _answerItem.getAnswer().
                getRespondent();
        this.target = _answerItem.getTarget();
        if (target == null) {
            throw new RuntimeException("target is null");
        }
        this.type = _answerItem.getQuestionItem().
                getQuestion().
                getType();
        this.question = _answerItem.getQuestionItem().
                getQuestion();
        propertyChange(null);
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        removeAllChildren();

        //hide relationship if question is hidden
        if (config.isDisplayRelationships(question)==false){
            return;
        }

        //hide if both source and target respondents are hidden
        if (config.isHideRespondentOrSex(source)==true && config.isHideRespondentOrSex(target)==true){
            return;
        }

        int displayFirstNRelationships = config.getDisplayFirstNRelationships();
        if (displayFirstNRelationships<6){ //при значении 6 считается что должны быть отображены все выборы, кривовато но пока так (TODO)
            if (displayFirstNRelationships<answerItem.getQuestionItem().getItemNumber()){
                    return;
            }
        }

        //display relationship
        Point2D start = config.getRespondentPosition(source);
        Point2D end = config.getRespondentPosition(target);

        Stroke stroke;
        if (type == MorenoQuestionType.Negative) {
            stroke = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f, new float[]{9, 4}, 0);
        } else {
            stroke = new BasicStroke();
        }
        vector = new PVector(
                start.getX(),
                start.getY(),
                end.getX(),
                end.getY(),
                17, 17,
                stroke,
                question.getColor());
        addChild(vector);
    }
}
